#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   18:19
# Author     : Y4
# Site       : 
# File       : 12 函数-map,filter,reduce.py
# Software   : PyCharm

# map 函数
def square(x):  # 计算平方数
    return x ** 2


result = map(square, [1, 2, 3, 4, 5])  # 计算列表各个元素的平方
print(list(result))

result = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
print(list(result))


# filter 函数
def is_odd(n):
    return n % 2 == 1


newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(newlist))

newlist = filter(lambda x: x % 2 == 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(list(newlist))


# reduce 函数
from functools import reduce
def add(x, y) :            # 两数相加
   return x + y
result1 =  reduce(add, [1,2,3,4,5])
print(result1)

result1 =  reduce(lambda x,y:x+y, [1,2,3,4,5])
print(result1)

